<?php

class PlaceCountriesController extends AppController{
    
    public $uses = array('Place', 'PlaceCountry', 'PlaceState');
    
     public $layout = 'place';
     
    public function view($id){
        $country = $this->PlaceCountry->find('first', array(
            'conditions' => array(
                'PlaceCountry.places_id' => $id
            ),
            'fields' => array(
            )
        ));
        
        $this->set('country', $country);
        
    }
    
    public function admin_index(){
        $this->layout = 'admin';
        if (!empty($this->request->data['PlaceCountry'])){
            $country = $this->request->data['PlaceCountry'];
            
            $id = $this->Place->save(array('Place' => array('type' => 'Country')), array('update' => 'false'));
            
            if ($id != null){
                $country['places_id'] = $id['Place']['id'];
                $this->PlaceCountry->save($country);
            }
        }
        
        $countries = $this->PlaceCountry->find('all', array(
            'recursive' => '-1'
        ));
        $this->set('countries', $countries);
    }
    
    
    public function admin_edit($idCountry){
        $this->layout = 'admin';
        if (!empty($this->request->data['PlaceState'])){
            $data = $this->request->data['PlaceState'];
            
            $id = $this->Place->save(array('Place' => array('type' => 'State')), array('update' => 'false'));
            
            if ($id != null){
                $data['places_id'] = $id['Place']['id'];
                $this->PlaceState->save($data);
            }
        }
        
        $data = $this->PlaceCountry->find('first', array(
            'conditions' => array(
                'places_id' => $idCountry
            ),
            'order' => 'PlaceCountry.name'
        ));
        
        $this->set('data', $data);
    }
    
    
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow( 'view');
    }
    
}
?>
